package com.lb.bs.client.processor.handle.dynamics.update;

import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.beans.LBItemBean;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.factory.SingletonFactory;
import com.lb.bs.client.processor.ProcessorHandleService;
import com.lb.bs.client.processor.beans.ProcessorDynamicHandlerBean;
import com.lb.bs.client.util.FieldUtil;

import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-19 17:23
 * description:
 */
@SingletonMark
public class UpdateProcessorHandle implements ProcessorHandleService<ProcessorDynamicHandlerBean> {

    @Override
    public void handleProcess(ProcessorDynamicHandlerBean dynamicBean) {

        String itemKey = dynamicBean.getKey();

        StoreCenter storeCenter = StoreCenter.getInstance();

        Map<String, LBItemBean> itemKeyMap = storeCenter.getItemKeyMap();

        if (!itemKeyMap.containsKey(itemKey)) {
            return;
        }
        LBItemBean item = itemKeyMap.get(itemKey);

        if (item == null) {
            return;
        }
        item.setValue(FieldUtil.transform(item.getField(), dynamicBean.getData()));
        item.setEnable(true);
        UpdateCallBackProcessorHandle callBackProcessorHandle = SingletonFactory.getInstance(UpdateCallBackProcessorHandle.class);
        callBackProcessorHandle.handleProcess(dynamicBean);
    }
}
